﻿using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MachineStatusClient
{
    public partial class HRUserControl : UserControl
    {
        private string EsopInitialDirectory = ConfigurationManager.AppSettings["EsopInitialDirectory"];
        public HRUserControl()
        {
            InitializeComponent();
        }

        private void button_HrSearch_Click(object sender, EventArgs e)
        {
            string initialDirectory = EsopInitialDirectory;
            if (!Directory.Exists(initialDirectory))
            {
                MessageBox.Show($"无法访问路径：{initialDirectory}\n请检查网络连接或路径配置",
                                "路径错误",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = initialDirectory;
            // 扩展过滤器：添加视频格式
            openFileDialog.Filter = "PDF文件|*.pdf|图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif|视频文件|*.mp4;*.avi;*.wmv;*.mov;*.mkv|所有文件|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                panel_hr.Controls.Clear();
                string filePath = openFileDialog.FileName;
                string fileExtension = Path.GetExtension(filePath).ToLower();

                // 根据文件类型选择播放方式
                if (IsVideoFile(fileExtension))
                {
                    try
                    {
                        //var player = new AxWMPLib.AxWindowsMediaPlayer();
                        //panel_hr.Controls.Add(player);  // 先添加
                        //player.CreateControl();           // 再创建
                        //player.Dock = DockStyle.Fill;
                        //player.URL = filePath;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"播放失败: {ex.Message}");
                    }
                    //// 使用Windows Media Player播放视频
                    //var player = new AxWMPLib.AxWindowsMediaPlayer();
                    //player.CreateControl();
                    //player.Dock = DockStyle.Fill;
                    //player.URL = filePath;
                    //panel_pdf.Controls.Add(player);

                }
                else
                {
                    // 使用ChromiumWebBrowser打开PDF/图片
                    ChromiumWebBrowser browser = new ChromiumWebBrowser(filePath);
                    browser.Dock = DockStyle.Fill;
                    //browser.MouseMove += Form3_MouseMove;
                    //browser.KeyDown += Form3_KeyDown;
                    panel_hr.Controls.Add(browser);
                }
            }
            // 尝试连接网络共享
            //if (!ConnectToNetworkShare())
            //{
            //    MessageBox.Show($"无法连接到网络共享: {networkShareRoot}");
            //    return;
            //}
            //// 设置初始目录为网络路径
            //string initialDirectory = @"D:\茂顺";
            ////string initialDirectory = EsopInitialDirectory;
            //if (!Directory.Exists(initialDirectory))
            //{
            //    MessageBox.Show($"无法访问路径：{initialDirectory}\n请检查网络连接或路径配置",
            //                    "路径错误",
            //                    MessageBoxButtons.OK,
            //                    MessageBoxIcon.Error);
            //    return;
            //}
            //// 创建打开文件对话框
            //OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.InitialDirectory = initialDirectory;
            //openFileDialog.Filter = "PDF文件|*.pdf|图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif|所有文件|*.*";
            //openFileDialog.FilterIndex = 1;
            //openFileDialog.RestoreDirectory = true;

            //// 显示对话框并检查用户是否选择了文件
            //if (openFileDialog.ShowDialog() == DialogResult.OK)
            //{
            //    panel_hr.Controls.Clear();
            //    ChromiumWebBrowser browser = new ChromiumWebBrowser(openFileDialog.FileName);
            //    browser.Dock = DockStyle.Fill;

            //    // 为动态控件添加事件
            //    //browser.MouseMove += Form3_MouseMove;
            //    //browser.KeyDown += Form3_KeyDown;
            //    // 解绑事件
            //    //browser.MouseClick += Form3_MouseClick;
            //    //browser.ControlAdded += Form3_ControlAdded;
            //    panel_hr.Controls.Add(browser);
            //}
        }

        // 判断是否为视频文件
        private bool IsVideoFile(string extension)
        {
            string[] videoExtensions = { ".mp4", ".avi", ".wmv", ".mov", ".mkv" };
            return videoExtensions.Contains(extension);
        }

        private void HRUserControl_Load(object sender, EventArgs e)
        {

        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            panel_hr.Controls.Clear();
        }
    }
}
